package com.scau.meetingbook.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
    //appid
    private final String APP_ID = "9021000149651284";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCR1XAIFmROoF9ItAP91XBxDM3AAPA/wwloVc10qRZHg/16PnUJu2gzSV5iWU/J3dlM9MxIY6nLNceiNDewKNMC9cycijbu/rggEEcdV8pYFtzwOcqI/Ds7+g37ZaUJkGvscFrBYuFAtlCTdW40/XkMp2EZsPNEFBLuFPHHb2LTWsCy29nX9z4nTro+/9U6YnjpObCepU6AD+5RVO/tW4QMQJkaDvTAGzW3q9dAe9ir6tIAtcc+J+fAXqcJ6wgeASdvKBb5paUmbMXBCUqHhKK5vnPgBgHWOdT8A7JOO669O8tIz8s+YmUbo46bpA8izTOyvDROxEqTY/4/FVUA2H2hAgMBAAECggEAfvPrn16+26PIzyXHA8Y3KHMY+jjW7OsA2Y84WIKKHp/MngqZIGvA/kS0obFWQEmczd2dqLb6LSkYLSCACh6/8ye/g9bek5ey+laxUd4HDEB6/bWRl0bc2g2I9n05+uV6zk3Q8+D8brVH19a2pfKy2k0rvo3IeE9xW0loYVGaGprqESfg2tD5N93V4RPG2tos9oiGXfoscgurD6npQa0QgY9+oxDcpwE3iWUTYSj2MYKZAOzTJ/RnYAEAWOtJYGC5lrXpxsa6WyseKOwnDhPniZevVhx5Pqd/qtLiceRPItdjlnrLaQrhpItUYC+ak6IP8WBTnNncOCA5lCAWg3bdQQKBgQDC9fsx9tNi3WwhMF9q6viyDvSA5i0z0gH28uKM93d+hpHTJi0hG0O8CWJFm6n/9CsOoDxFj6BfJkcIXKAvzBOuzKm0LSOV0CUGuDHBCiF0Pb4VmFcn5fq7RjdnEF1FPZOu8Wch8DVZiuiW8VmDXDj7PsAZgIdHA9+QSqQlPw8mlQKBgQC/ffD2RD5iZsTCc4s35h9YUoTPV/mzIkC83BUMzzOBf7lKSy+iiY0xX1Q6FLEA7eoJ5hfiVMVQwx71jqNNlE9RXanNH3bD5AyU9oYok1xxkb2YdguBKz1wUcJmCQ1FaHAZsdKoLOObDG1jxXu7BQl6791IWo2remz/v88+NSKz3QKBgEi1JcYfb/wp6Z6DBMLB+JenHiZIMUlnI5ZuRHEgBS9pjk65qRD0sxEYSs7826x56ejddzzV8JArnUgQCyloKVsCNrPcNa40XuqfnOaKGanZMN+q3a4ihZx79kN4heBBs6cFnlm41JydQlqwjZqAtTt1znx0Q38HgcO1InxFhSf1AoGBAInpCwRha8h0mRouGsK8YjC65Vlq07OWZKdfnW1nQIHwWaLO+VSG2gAH1uIpirzPm7FbXDItuRmMHRzeiG/T8zf3S9Yl5eP4trMFxOMuFfLY8oH96hUx7gKZb3cK59rn92wZ7EwgYB+HWLT1QkITw5sdavGz/bnU5vFd08TKgd6lAoGAEfCXw3ek8LxfW67Kdafgl1YBuYa9Snb5bRFoc82d3pxj3y6umVUY6GsCkzcbkdXWJoYUssdxNyaGxn3UY6iFPwGlBFBUTxuhf62JaC6H1VBHnMNcvOif4EPIRTKe8l2GqwunXYwAvG72FNqtBeT/utXemwXWXDSD6cKiVxkUtVo=";
    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiqPpMtT6hts7pJIyUJhCnayQI4eztiXlDCtcQFN+WV6xZfSjZr9olW2DpwgEOAoTdZRvuQ9Zx4nOd6ON0/jslQsak5W0nCk8cnxi+JKJV2c0siMzNDbVYXCgP3gOAsRXNydxX9knfhsMfQhs5G3FHoujHReihi2c/XH3I9Yl9ysKztDROPqEpiim5Y/RGo3bELpiuYHbpd5cPuFePa/wn0n9zJggv88xqtQRfMxfcyejFdhxYLh0K4cyMWcOXanFHWVQ6TfLfsQEl6iCCiQFKdpm8rPJ1MYGVEcVbhazyH06jy3zgG8w3KGfc9EVXOWK4sN52D4NYusSUhzk/FGb4wIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://y8ef464a.natappfree.cc/alipay/notify";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8787/#/customer/orderList";
    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);


        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
